//
//  DJHomeViewController.swift
//  DJWeibo
//
//  Created by demo on 2017/2/7.
//  Copyright © 2017年 demo. All rights reserved.
//

import UIKit

//定义全局常量，最好使用private修饰
fileprivate let cellId = "cellId"


class DJHomeViewController: DJBaseViewController {
    
    //微博数据数组
    //fileprivate lazy var statusList = [String]()
    
    //列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //加载数据
    override func loadData() {
        
        print("准备刷新最后一条\(self.listViewModel.statusList.last?.text)")
        listViewModel.loadStatus(pullUp: self.isPullup) { (isSuccess,hasMorePullup) in
            print("加载数据结束")
            
            //结束刷新
            self.refreshControl?.endRefreshing()
            
            //恢复上拉刷新标记
            self.isPullup = false

            //刷新表格
            if hasMorePullup{
                self.tableView?.reloadData()
            }

        }
        
        //用网络工具加载微博数据
        //controller中不关心网络请求的实现，把网络请求部分封装成一个方法，controller直接使用返回的数据
//       WBNetworkManager.shared.statusList { (list, isSuccess) in
//        //字典转模型，绑定表格数据
//        print(list)
//        }
//        
//        //加载数据
//        print("开始加载数据\(WBNetworkManager.shared)")
//        
//        //模拟延时加载数据
//        //DispatchTime.now 是指现在，后面“+数字“ 可实现延迟加载
//        //使用GCD线程由队列调度
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
//            for i in 0..<10 {
//                if self.isPullup{
//                    self.statusList.append("上拉\(i)")
//                }else{
//                //将数据插入到数组顶部
//                self.statusList.insert(i.description, at: 0)
//                }
//            }
////            //刷新表格
////            print("刷新表格")
////            
////            //结束刷新
////            self.refreshControl?.endRefreshing()
////            
////            //恢复上拉刷新标记
////            self.isPullup = false
////            
////            self.tableView?.reloadData()
//        }
//        
    }
    
    //显示好友
    @objc fileprivate func showFriends(){
        print("好友")
    
        let vc = DJDemoViewController()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
        
    }
}

//MARK: -表格数据源方法, 集体的数据源方法实现，不需要super
extension DJHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1、取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2、设置cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3、返回cell
        return cell
        
    }
}

extension DJHomeViewController{
    override func setupTableView() {
       super.setupTableView()
        //设置导航栏按钮
    //    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        //swift调用OC返回instancetype的方法，判断不出是否可选，btn后加个类型解决
        /*
        let btn:UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:btn)//或者用.init(customView:btn)
 */
         navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
    }
}
